<template>
  <div>


    <el-table
        :data="tableData"
        
        row-key="id"
        border
        default-expand-all
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        v-if="hasAuth('get/jurisdiction')"
    >

      <el-table-column
          prop="title"
          label="名称"
          sortable
          width="180">
      </el-table-column>
      <el-table-column
          prop="permission_code"
          label="权限编码"
          sortable
          width="180">
      </el-table-column>

      <el-table-column
          prop="jurisdiction_type"
          label="类型">
        <template slot-scope="scope">
          <el-tag size="small" v-if="scope.row.jurisdiction_type === 0">目录</el-tag>
          <el-tag size="small" v-else-if="scope.row.jurisdiction_type ===1 " type="success">菜单</el-tag>
          <el-tag size="small" v-else-if="scope.row.jurisdiction_type === 2" type="info">按钮</el-tag>
        </template>

      </el-table-column>

      <el-table-column
          prop="url"
          label="菜单URL">
      </el-table-column>
      <el-table-column
          prop="name"
          label="菜单组件">
      </el-table-column>
<!--      <el-table-column-->
<!--          prop="state"-->
<!--          label="状态">-->
<!--        <template slot-scope="scope">-->
<!--          <el-tag size="small" v-if="scope.row.state === 0" type="success">正常</el-tag>-->
<!--          <el-tag size="small" v-else-if="scope.row.state === 1" type="danger">禁用</el-tag>-->
<!--        </template>-->

<!--      </el-table-column>-->
<!--      <el-table-column-->
<!--          prop="state"-->
<!--          label="操作">-->

<!--        <template slot-scope="scope">-->
<!--          <el-button type="text" @click="editHandle1(scope.row.id)" v-if="hasAuth('update/jurisdiction')">编辑</el-button>-->
<!--          <el-divider direction="vertical"></el-divider>-->

<!--            <el-popconfirm title="这是一段内容确定删除吗？" @confirm="delHandle(scope.row.id)">-->
<!--              <el-button type="text" slot="reference" v-if="hasAuth('delete/jurisdiction')">删除</el-button>-->
<!--            </el-popconfirm>-->

<!--        </template>-->
<!--      </el-table-column>-->

    </el-table>


    <!--新增对话框-->
    <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="600px"
        :before-close="handleClose">

      <el-form :model="editForm" :rules="editFormRules" ref="editForm" label-width="100px" class="demo-editForm">

        <el-form-item label="上级菜单" prop="parentId">
          <el-select v-model="editForm.parent_id" placeholder="请选择上级菜单">
            <template v-for="item in tableData">
              <el-option :label="item.title" :value="item.id"></el-option>
              <template v-for="child in item.children">
                <el-option :label="child.title" :value="child.id">
                  <span>{{ "- " + child.title }}</span>
                </el-option>
              </template>
            </template>
          </el-select>
        </el-form-item>

        <el-form-item label="菜单名称" prop="title" label-width="100px">
          <el-input v-model="editForm.title" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="权限编码" prop="permission_code" label-width="100px">
          <el-input v-model="editForm.permission_code" autocomplete="off"></el-input>
        </el-form-item>


        <el-form-item label="菜单URL" prop="url" label-width="100px">
          <el-input v-model="editForm.url" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="菜单组件" prop="name" label-width="100px">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="类型" prop="jurisdiction_type" label-width="100px">
          <el-radio-group v-model="editForm.jurisdiction_type">
            <el-radio :label=0>目录</el-radio>
            <el-radio :label=1>菜单</el-radio>
            <el-radio :label=2>按钮</el-radio>
          </el-radio-group>
        </el-form-item>

<!--        <el-form-item label="状态" prop="state" label-width="100px">-->
<!--          <el-radio-group v-model="editForm.state">-->
<!--            <el-radio :label=1>禁用</el-radio>-->
<!--            <el-radio :label=0>正常</el-radio>-->
<!--          </el-radio-group>-->
<!--        </el-form-item>-->
        <el-form-item>
          <el-button type="primary" @click="submitForm('editForm')">立即创建</el-button>
          <el-button @click="resetForm('editForm')">重置</el-button>
        </el-form-item>
      </el-form>

    </el-dialog>

    <!--修改对话框-->
    <el-dialog
        title="提示"
        :visible.sync="dialogVisible1"
        width="600px"
        :before-close="handleClose">

      <el-form :model="editForm" :rules="editFormRules" ref="editForm" label-width="100px" class="demo-editForm">

        <el-form-item label="上级菜单" prop="parentId">
          <el-select v-model="editForm.parent_id" placeholder="请选择上级菜单">
            <template v-for="item in tableData">
              <el-option :label="item.title" :value="item.id"></el-option>
              <template v-for="child in item.children">
                <el-option :label="child.title" :value="child.id">
                  <span>{{ "- " + child.title }}</span>
                </el-option>
              </template>
            </template>
          </el-select>
        </el-form-item>

        <el-form-item label="菜单名称" prop="title" label-width="100px">
          <el-input v-model="editForm.title" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="权限编码" prop="permission_code" label-width="100px">
          <el-input v-model="editForm.permission_code" autocomplete="off"></el-input>
        </el-form-item>


        <el-form-item label="菜单URL" prop="url" label-width="100px">
          <el-input v-model="editForm.url" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="菜单组件" prop="name" label-width="100px">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="类型" prop="jurisdiction_type" label-width="100px">
          <el-radio-group v-model="editForm.jurisdiction_type">
            <el-radio :label=0>目录</el-radio>
            <el-radio :label=1>菜单</el-radio>
            <el-radio :label=2>按钮</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="状态" prop="state" label-width="100px">
          <el-radio-group v-model="editForm.state">
            <el-radio :label=1>禁用</el-radio>
            <el-radio :label=0>正常</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm1(editForm.id)">立即修改</el-button>
          <el-button @click="resetForm('editForm')">重置</el-button>
        </el-form-item>
      </el-form>

    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "Permission",
  data() {
    return {
      dialogVisible: false,
      dialogVisible1: false,
      editForm: {},
      editFormRules: {
        title: [
          {required: true, message: '请输入名称', trigger: 'blur'}
        ],
        permission_code: [
          {required: true, message: '请输入权限编码', trigger: 'blur'}
        ],
        state: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ],
        parent_id: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ]
      },
      tableData: [],
    }
  },
  mounted() {
    this.getMenuTree()
  },
  methods: {
    getMenuTree() {
      this.$axios.get(this.$settings.BASE_URL + 'jurisdiction/jurisdiction/').then(res => {
        this.tableData = res.data.data
        console.log(this.tableData, '--------------------------------------')
      })
    },
    //创建按钮
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post(this.$settings.BASE_URL + 'jurisdiction/jurisdictioncreate/', this.editForm)
              .then(res => {
                if (res.data.code == 100) {
                  this.$message({
                    showClose: true,
                    message: '恭喜你，操作成功',
                    type: 'success',
                    onClose: () => {
                      this.getMenuTree()
                    }
                  });

                  this.dialogVisible = false
                } else {
                  this.$message.error('添加失败');
                }


              })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    //添加按钮数据
    editHandle1(id) {
      this.$axios.get(this.$settings.BASE_URL + 'jurisdiction/jurisdiction/' + id + '/').then(res => {
        this.editForm = res.data.data

        this.dialogVisible1 = true
      })
    },
    // 修改按钮
    submitForm1(id) {
      this.$axios.put(this.$settings.BASE_URL + 'jurisdiction/jurisdictionupdate/' + id + '/', this.editForm)
          .then(res => {
            if (res.data.code == 100) {
              this.$message({
                showClose: true,
                message: '恭喜你，操作成功',
                type: 'success',
                onClose: () => {
                  this.getMenuTree()
                }
              });

              this.dialogVisible1 = false
            } else {
              this.$message.error('添加失败');
            }


          })
    },
    //重置按钮
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogVisible = false
      this.dialogVisible1 = false
      this.editForm = {}
    },
    handleClose() {
      this.resetForm('editForm')
    },
    //删除按钮
    delHandle(id) {
      this.$axios.delete(this.$settings.BASE_URL + "jurisdiction/jurisdictiondelete/" + id + '/').then(res => {
        if (res.data.code == 100) {
          this.$message({
            showClose: true,
            message: '恭喜你，操作成功',
            type: 'success',
            onClose: () => {
              this.getMenuTree()
            }
          });
        } else {
          this.$message.error('添加失败');
        }


      })
    }
  }
}
</script>

<style scoped>

</style>
